<?php
// include("db.php");
require('db.php');
include("../wall/biaoqing.php");
if (isset($_GET['do'])) {
    $do = $_GET['do'];
} else {
    die("invild action");
}
$ppluserinfoc = new M('paipaile_user');
$ppluserinfo = $ppluserinfoc->find('openid="'.$openid.'"');

call_user_func($do);
/**
 * 拍拍乐回复函数$pplconf['pplnum']为拍拍乐当前状态
 * $pplconf['pplnum']=1表示未上传
 * $pplconf['pplnum']=2表示上传中
 * $pplconf['pplnum']=3表示投票中
 * *$check[2]=4表示用户当前是否处于拍拍乐模式
 * $ppluserinfo['status']表示该用户的投票状态 0表示该用户未投票
 * $ppluserinfo['status']表示该用户的投票状态 1表示该用户已经投过票
 * * $ppluserinfo['status']表示该用户的投票状态 2表示该用户未投票，但已经上传过照片，不能再上传照片
 * */
function querystart()
{
    global $xuanzezu, $pplconf;
    $ppluserinfoc = new M('paipaile_user');
    if ($pplconf['pplnum'] == 3) {
        echo 1;
    } else {
        $ppluserinfoc->query("UPDATE `weixin_paipaile_config` SET `pplnum` = 2");
        echo 0;
    }
}

function getppldate()
{
    global $xuanzezu, $pplconf;
    $paipaile_m = new M('paipaile');
    if ($pplconf['pplnum'] == 3) {
        $sum = $paipaile_m->find("id>0", "sum(voteres) as s");
        $sum = $sum['s'];
        $rows = $paipaile_m->select("id>0  order by voteres desc");
        for ($i = 0, $l = count($rows); $i < $l; $i++) {
            $row1 = $rows[$i];
            $row1['nickname'] = pack('H*', $row1['nickname']);
            $row1 = emoji_unified_to_html(emoji_softbank_to_unified($row1));
            $row1['nickname'] = $row1['nickname'];
            $row1['progress'] = sprintf("%.2f", ($row1['voteres'] / $sum) * 100);
            $ppl_check[] = $row1;
        }
        echo json_encode($ppl_check);
    }
}

/**
 * 抽奖函数
 * */
function getcjinfo()
{
    $ppluserinfoc = new M('paipaile_user');
    // echo var_export($ppluserinfoc);
    $ppluserinfoc->query("UPDATE `weixin_paipaile_config` SET `pplnum` = 1");
    $rows = $ppluserinfoc->select('status=1 or status=2');
    // $data = mysql_query("select * from weixin_paipaile_user where status=1 or status=2");
    for ($i = 0, $l = count($rows); $i < $l; $i++) {
        $row1 = $rows[$i];
        $row1['nickname'] = pack('H*', $row1['nickname']);
        $row1 = emoji_unified_to_html(emoji_softbank_to_unified($row1));
        $arr[] = array(
            'key' => $i,
            'id' => $row1['id'],
            'openid' => $row1['openid'],
            'avatar' => $row1['avatar'],
            'nickname' => $row1['nickname'],
            'from' => $row1['fromtype'],
        );
    }

    //    $i=0;
    //    while($row1=mysql_fetch_array($data)){
    //  $row1['nickname']=pack('H*',$row1['nickname']);
    //  $row1=emoji_unified_to_html(emoji_softbank_to_unified($row1));
    //    $arr[] = array(
    //        'key'=>$i,
    //      'id' => $row1['id'],
    //      'openid' => $row1['openid'],
    //      'avatar' => $row1['avatar'],
    //      'nickname' => $row1['nickname'],
    //      'from' => $row1['fromtype'],
    //    	);
    //    	$i++;
    // }
    echo json_encode($arr);
}

/**
 * 写入中奖信息
 * */
function posttocopy()
{
    global $pplconf;
    $zjuser = $_POST['zjuser'];
    $zjc = new M('paipaile_zjuser');
    $paipaile_user_m=new M('paipaile_user');
    // echo var_export($zjuser);
    foreach ($zjuser as $V) {
        // echo 1;
        $data = $paipaile_user_m->find("`openid`='" . $V . "'");
        // $data = mysql_query("select * from `weixin_paipaile_user` where `openid`='$V'");

        $arr = $data;//mysql_fetch_array($data);
        $zjc->add(array("NULL", $arr['openid'], $arr['nickname'], $arr['avatar'], $arr['fromtype'], 1, null, $pplconf['pplround'], null, null));
        /**
         * 已经中奖用户拉黑处理
         * */
        if ($pplconf['black_switch']) {
            $zjc->query('UPDATE `weixin_paipaile_user` SET `status` = 3 where `openid`="'.$V.'"');
            // mysql_query("UPDATE `weixin_paipaile_user` SET `status` = 3 where `openid`='$V'");
        }
    }
    $paipailec = new M('paipaile');
    $paipaile = $paipailec->select("id>0 order by voteres desc  LIMIT " . $pplconf['pplvotenum']);
    foreach ($paipaile as $V) {
        $zjc->add(array("NULL", $V['openid'], $V['nickname'], $V['avatar'], $V['fromtype'], 2, null, $pplconf['pplround'], $V['picurl'], $V['voteres']));
    }
    $paipailec->query("UPDATE `weixin_paipaile_config` SET `pplround` = `pplround`+1");
    $paipailec->query("UPDATE `weixin_paipaile_user` SET `status` = 0 where `status` != 3");
    $paipailec->query("TRUNCATE TABLE weixin_paipaile");
    echo 'ok';
}

?>